package cn.com.pxto.web.action;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;

import cn.com.pxto.web.excel.ReadExcel;

public class ExcelAction extends DispatchAction {
	private Log log = LogFactory.getLog(ExcelAction.class);
	
	private ReadExcel readExcel;

	public ActionForward preUploadFile(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in preUploadFile method");
		return mapping.findForward("preUploadFile");
	}

	public ActionForward doUploadFile(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in doUploadFile method");
		if (form instanceof UploadFileForm) {// 如果form是uploadsForm
			String encoding = request.getCharacterEncoding();
			if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) {
				response.setContentType("text/html; charset=utf-8");// 如果没有指定编码，编码格式为gb2312
			}
			UploadFileForm theForm = (UploadFileForm) form;
			FormFile file = theForm.getTheFile();// 取得上传的文件
			try {
				InputStream stream = file.getInputStream();// 把文件读入
				String filePath = request.getRealPath("/excel");// 取当前系统路径
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				String pattern = "yyyy-MM-dd-HH-hh";
				SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
				String fileName = dateFormat.format(new Date());
				OutputStream bos = new FileOutputStream(filePath 
						+"\\"+fileName+".xls");// 建立一个上传文件的输出流
				log.info(filePath+"\\"+fileName+".xls");
				int bytesRead = 0;
				byte[] buffer = new byte[8192];
				while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
					bos.write(buffer, 0, bytesRead);// 将文件写入服务器
				}
				bos.close();
				stream.close();
				try {
					readExcel.loadExcelToDb(filePath + "\\" + fileName + ".xls");
				} catch (Exception e) {
					log.error("载入文件错误！", e);
					request.setAttribute("msg", "上传文件错误!");
					return mapping.findForward("return");
				}
			} catch (Exception e) {
				log.error("上传文件错误！", e);
				request.setAttribute("msg", "上传文件错误!");
				return mapping.findForward("return");
			}
			// request.setAttribute("dat",file.getFileName());
			request.setAttribute("msg", "上传文件成功!");
			return mapping.findForward("return");
		}
		return null;
	}

	public void setReadExcel(ReadExcel readExcel) {
		this.readExcel = readExcel;
	}
}
